home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 48 / Amiga Format CD48 (1999-12-13)(Future Publishing)(GB)(Track 1 of 2)[!][issue 2000-01].iso / -serious- / programming / c / supralib / developer / source / recdirinit.c < prev    next >
C/C++ Source or Header  |  1999-11-01  |  4KB  |  140 lines

  1. /****** RecDirInit ******************************************
  2. *
  3. *   NAME
  4. *       RecDirInit -- Initializes recursive files scanning process (V10)
  5. *       (dos V36)
  6. *
  7. *   SYNOPSIS
  8. *       error = RecDirInit(RecDirInfo)
  9. *
  10. *       UBYTE = RecDirInit(struct RecDirInfo *)
  11. *
  12. *   FUNCTION
  13. *       This function is required to start scanning files through entire
  14. *       or partial directory tree. It locks a directory path provided in
  15. *       RecDirInfo structure, then files can be examined by calling
  16. *       RecDirNext() function. Please see RecDirNext() for more
  17. *       explanation on how this is useful.
  18. *       You should initialize RecDirInfo by yourself, and you MUST set
  19. *       rdi_Path, rdi_Num, and rdi_Pattern.
  20. *
  21. *   INPUTS
  22. *       RecDirInfo - pointer to RecDirInfo structure, which should be
  23. *       allocated and initialized before RecDirInit() is called.
  24. *       You must set its rdi_Path field to starting directory path
  25. *       you want to scan.
  26. *       
  27. *       Set rdi_Num for maximum number of directories you wish to scan
  28. *       into. If you set rdi_Num to 1 it will only scan one level (that
  29. *       rdi_Path points to). If you set rdi_Num to -1 it will scan
  30. *       unlimited number of subdirectories deep.
  31. *
  32. *       If rdi_Pattern field is non-NULL and points to a string then
  33. *       calling RecDirNext will only return files that match the
  34. *       pattern string. NOTE that rdi_Pattern should point to a string
  35. *       which has been parsed with ParsePattern().
  36. *
  37. *   RESULT
  38. *       error - 0 if no error, otherwise returns one of the following
  39. *       errors (also see libraries/supra.h):
  40. *           RDI_ERR_FILE - Path provided in rdi_Path points to a file
  41. *                          not directory.
  42. *           RDI_ERR_NONEXIST - Path provided in rdi_Path does not exist.
  43. *           RDI_ERR_MEM - not enough memory to execute RecDirInit().
  44. *
  45. *   EXAMPLE
  46. *       Please see an example in RecDirNext() function.
  47. *
  48. *   NOTES
  49. *       IMPORTANT: You MUST open dos.library before calling RecDirInit()!
  50. *       rdi_Path is a path relative to a current path your program uses.
  51. *       That means you can set rdi_Path to "" to scan from current
  52. *       directory, or "/" to scan parent directory.
  53. *
  54. *   BUGS
  55. *       None found yet.
  56. *
  57. *   SEE ALSO
  58. *       RecDirFree(), RecDirNext(), libraries/supra.h
  59. *
  60. *   CHANGES
  61. *       littel changes for use with vbcc
  62. *
  63. **************************************************************************/
  64.  
  65. #include <proto/exec.h>
  66. #include <proto/dos.h>
  67. #include <exec/memory.h>
  68. #include <string.h>
  69. #include <libraries/supra.h>
  70.  
  71. UBYTE RecDirInit(struct RecDirInfo *rdi)
  72. {
  73.     BPTR lock;
  74.     struct FileInfoBlock *fib;
  75.     struct LockNode *ln;
  76.     char *lnPath;
  77.     int len;
  78.  
  79.     lock = Lock(rdi->rdi_Path, ACCESS_READ);
  80.     if (!lock) return(RDI_ERR_NONEXIST);
  81.  
  82.     fib = AllocMem(sizeof(struct FileInfoBlock), 0L);
  83.     if (fib)
  84.     {
  85.         if (Examine(lock, fib))
  86.         {
  87.             if (fib->fib_DirEntryType > 0)        /* Directory */
  88.             {
  89.                 ln = AllocMem(sizeof(struct LockNode), 0L);
  90.                 if (ln)
  91.                 {
  92.                     /* Everything all right till now */
  93.                     /* Now first prepare to copy a path */
  94.  
  95.                     lnPath = AllocMem(strlen(rdi->rdi_Path)+2, 0L);
  96.                     if (lnPath)
  97.                     {
  98.                         strcpy(lnPath, rdi->rdi_Path);
  99.  
  100.                         /* Alter path's ending: check for slash etc. */
  101.                         len = strlen(lnPath);
  102.                         if (len > 0)
  103.                         {
  104.                             if (lnPath[len-1] != '/' && lnPath[len-1] != ':')
  105.                             {
  106.                                 strcat(lnPath,"/");
  107.                             }
  108.                         }
  109.  
  110.                         ln->ln_Succ = NULL;
  111.                         ln->ln_Pred = NULL;
  112.                         ln->ln_FIB= fib;
  113.                         ln->ln_Lock = lock;
  114.                         ln->ln_Path = lnPath;
  115.                         ln->ln_Len= strlen(rdi->rdi_Path)+2;
  116.  
  117.                         rdi->rdi_Node = ln;
  118.                         rdi->rdi_Deep = 1;
  119.                         return(0);
  120.                     }
  121.                 }
  122.             }
  123.             else if (fib->fib_DirEntryType < 0)        /* Path is file */
  124.             {
  125.                 FreeMem(fib, sizeof(struct FileInfoBlock));
  126.                 UnLock(lock);
  127.                 return(RDI_ERR_FILE); /* Indicate that path is a file not dir */
  128.             }
  129.         }
  130.     }
  131.  
  132.     if (ln) FreeMem(ln,sizeof(struct LockNode));
  133.     if (fib) FreeMem(fib, sizeof(struct FileInfoBlock));
  134.     if (lock) UnLock(lock);
  135.  
  136.     return(RDI_ERR_MEM); /* No success */
  137. }
  138.  
  139.  
  140.